package com.zhuoyue.biz.core.api.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhuoyue.biz.core.api.dto.rep.SolVoiceSetSceneRepDTO;
import com.zhuoyue.common.exception.ErrorType;
import com.zhuoyue.common.exception.ZyServerException;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuoyue.biz.core.api.dao.SetSolVoiceSceneMapper;
import com.zhuoyue.biz.core.api.domain.SetSolVoiceScene;

/**
 * @author wjming
 */
@Service
public class SetSolVoiceSceneService extends ServiceImpl<SetSolVoiceSceneMapper, SetSolVoiceScene> {
    @Autowired
    private SetSolVoiceSceneMapper setSolVoiceSceneMapper;

    public SolVoiceSetSceneRepDTO saveMessage(SetSolVoiceScene setSolVoiceScene) {


        boolean save = save(setSolVoiceScene);
        if (!save) {
            throw new ZyServerException(ErrorType.Service_Fail);
        }
        SetSolVoiceScene byId = getById(setSolVoiceScene.getId());
        SolVoiceSetSceneRepDTO solVoiceSetSceneRepDTO = new SolVoiceSetSceneRepDTO().doToDto(byId);
        return solVoiceSetSceneRepDTO;
    }

    /**
     * 获取语音场景信息列表
     *
     * @param voiceId
     * @return
     */
    public List<SolVoiceSetSceneRepDTO> getList(String voiceId) {
        List<SetSolVoiceScene> list = list(Wrappers.<SetSolVoiceScene>lambdaQuery()
                .eq(SetSolVoiceScene::getVoiceId, voiceId));
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        List<SolVoiceSetSceneRepDTO> solVoiceSetSceneReps = new ArrayList<>(8);

        list.forEach(l -> {
            SolVoiceSetSceneRepDTO solVoiceSetSceneRepDto = new SolVoiceSetSceneRepDTO().doToDto(l);
            solVoiceSetSceneReps.add(solVoiceSetSceneRepDto);
        });
        return solVoiceSetSceneReps;
    }

    /**
     * 修改场景关系信息
     *
     * @param setSolVoiceScene
     * @return
     */
    public SolVoiceSetSceneRepDTO putSceneRelation(SetSolVoiceScene setSolVoiceScene) {

        boolean update = updateById(setSolVoiceScene);
        if (!update) {
            throw new ZyServerException(ErrorType.Service_Fail);
        }
        SetSolVoiceScene byId = getById(setSolVoiceScene.getId());
        SolVoiceSetSceneRepDTO solVoiceSetSceneRepDTO = new SolVoiceSetSceneRepDTO().doToDto(byId);
        return solVoiceSetSceneRepDTO;
    }

    /**
     * 查询编码
     *
     * @param voiceId
     * @return
     */
    public List<Integer> selectCode(String voiceId) {
        return setSolVoiceSceneMapper.selectCodeByVoiceId(voiceId);
    }
}



